import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;



public class Lidstone implements ProbablisticModel {

	
	//data members
	private static final long V = 300000;
	
	private Corpus corpus; //corpus of words
	private double lambda = 0; //lambda parameter of Lidstone model
	
	/**
	 * CTOR
	 * @param c
	 */
	public Lidstone(Corpus c) {
		corpus = c;
	}
	
	/**
	 * Sets the lambda parameter
	 * @param lambda
	 */
	public void setLambda(double lambda) {
		this.lambda = lambda;
	}
	
	/**
	 * Calculates the probability according to Lidstone smoothing method
	 */
	public double getProbablity(String word) {
		double C = corpus.C(word);
				
		return (C + lambda) / (corpus.S() + lambda * V);
	}
	
	public double debugModel() {
		double sum = 0;
		
		for (String word : corpus.getUniqueWordSet()) {
			sum += getProbablity(word);
		}
		
		sum += (V - corpus.X()) * (lambda / (corpus.S() + lambda*V));
		
		return sum;
	}
		
	public double getLikelihood(String fileName) throws IOException{
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		
		double sum = 0;
		int wordCounter = 0;
		String line = null;
		 
		while ((line = reader.readLine()) != null) {
			String[] parsed = line.split(" ");

			for (String word : parsed) {
				double prob = getProbablity(word);
				sum += Math.log(prob);
				++wordCounter;
			}
		}
		
		reader.close();
		
		double exp = 1.0 / wordCounter * sum;
		
		
		return Math.pow(2, -exp);
	}
}
